package com.miplus.ccrm.core.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author： Yaohui
 * @Date： 2024/11/23 18:09
 * @Describe：
 */
@Configuration
@EnableAsync
public class TaskPoolConfig {

//    @Value("${hrbank.syncTaskExecutor.corePoolSize}")
//    private int corePoolSize;
//    @Value("${hrbank.syncTaskExecutor.maxPoolSize}")
//    private int maxPoolSize;
//    @Value("${hrbank.syncTaskExecutor.queueCapacity}")
//    private int queueCapacity;
//    @Value("${hrbank.syncTaskExecutor.keepAliveSeconds}")
//    private int keepAliveSeconds;

    @Bean("syncTaskExecutor")
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//核心线程数目 10
        executor.setMaxPoolSize(20);//指定最大线程数 20
        executor.setQueueCapacity(200);//队列中最大的数目  200
        executor.setKeepAliveSeconds(60);//线程空闲后的最大存活时间  60
        executor.setThreadNamePrefix("sync-task-");//线程名称前缀
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }

}
